package com.example.csdn.service.impl;

import com.example.csdn.mapper.UserMapper;
import com.example.csdn.pojo.User;
import com.example.csdn.service.UserService;
import com.example.csdn.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User findByUserName(String username) {
        return userMapper.findByUserName(username);
    }

    @Override
    public void register(User user) {
        user.setIdentity("普通用户");
        user.setStatus("启用");
        user.setUserDes("测试人员");
        userMapper.add(user);
    }

    @Override
    public User getUserInfo(Integer userId) {
        return userMapper.getUserInfo(userId);
    }

    @Override
    public void updateUserInfo(User user) {
        Map<String ,Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");
        user.setId(userId);
        userMapper.updateUserInfo(user);
    }

    @Override
    public void updateAvatar(String avatarURL) {
        Map<String ,Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");
        userMapper.updateAvatar(avatarURL,userId);
    }

    @Override
    public void updatePwd(String newPwd) {
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        userMapper.updatePwd(newPwd,id);
    }
}
